package com.sxtbbs.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sxtbbs.bean.BBSBlog;
import com.sxtbbs.bean.BBSBoard;
import com.sxtbbs.bean.BBSChildboard;
import com.sxtbbs.bean.BBSReplyBlog;
import com.sxtbbs.bean.BBSUser;
import com.sxtbbs.dao.UserDao;
import com.sxtbbs.util.BaseServlet;
import com.sxtbbs.util.MD5Encryption;
import com.sxtbbs.util.UserDaoFactory;

public class UserService extends BaseServlet {

	private static final long serialVersionUID = 1L;
	
	private UserDao userDao = UserDaoFactory.getUserDao();

	public String index(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<BBSBoard> boardList = new ArrayList<BBSBoard>();
		boardList = userDao.getBoard();
		List<BBSChildboard> bbsChildboardList = new ArrayList<BBSChildboard>();
		bbsChildboardList = userDao.getChildBoard();
		request.setAttribute("childboardlist", bbsChildboardList);
		request.setAttribute("boardList", boardList);
		return "f:/index.jsp";
	}
	
	public String login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uname = request.getParameter("uname");
		String upass = request.getParameter("upass");
		BBSUser bbsUser = userDao.getUser(uname);
		if(bbsUser != null) {
			if(uname.equals(bbsUser.getUuname()) 
					&& MD5Encryption.MD5Encode(upass).equals(bbsUser.getUpass())) {
				request.getSession().setAttribute("uname", uname);
				return "r:/userservice?method=index";
			} else {
				return "r:/login.jsp?errorcode=1";
			}
		}
		return "r:/login.jsp?errorcode=2";
	}
	
	public String reg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uuname = request.getParameter("uname");
		String upass = request.getParameter("upass");
		String uprov = request.getParameter("prov") == "this.value" ? request.getParameter("prov") : "-1";
		String ucity = request.getParameter("city") == "this.value" ? request.getParameter("city") : "-1";
		String ugender = request.getParameter("gender")=="1" ?"男":"女";
		String uphoto = request.getParameter("head");
		BBSUser bbsUser = new BBSUser();
		bbsUser.setUuname(uuname);
		bbsUser.setUpass(upass);
		bbsUser.setUgender(ugender);
		bbsUser.setUprov(uprov);
		bbsUser.setUcity(ucity);
		bbsUser.setUphoto(uphoto);
		userDao.addUser(bbsUser);
		HttpSession session = request.getSession();
		session.setAttribute("uname", uuname);
		return "r:/userservice?method=index";
	}
	
	public String listBlog(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageNo = 1;
		String cboid = request.getParameter("cboid");
		String cboname = request.getParameter("cboname");
		List<BBSBlog> bbsBlogList = new ArrayList<BBSBlog>();
		int pageSize = Integer.parseInt(this.getServletContext().
				getInitParameter("pagesize").toString());
		
		String startPage = request.getParameter("pageno");
		int totalBlog = userDao.getTotalBlog(cboid);
		int totalPage = (totalBlog % pageSize == 0) ? (totalBlog / pageSize) : (totalBlog / pageSize + 1); 
		if(startPage == null || startPage.trim().equals("")) {
			pageNo = 1;
		} else {
			pageNo = Integer.parseInt(startPage);
		}
		if(pageNo <= 1) {
			pageNo = 1;
		}
		if(pageNo >= totalPage) {
			pageNo = totalPage;
		}
		int startPos = (pageNo - 1) * pageSize;
		bbsBlogList = userDao.getBlog(cboid, startPos, pageSize);
		request.setAttribute("pageno", pageNo);
		request.setAttribute("totalpage", totalPage);
		request.setAttribute("cboid", cboid);
		request.setAttribute("bloglist", bbsBlogList);
		request.setAttribute("cboname", cboname);
		return "f:/list.jsp";
	}
	
	public String blogDetail(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String bloid = request.getParameter("bloid");
		int pageNo = 1;
		int startPos = 0;
		int pageSize = Integer.parseInt(this.getServletContext()
				.getInitParameter("pagesize"));
		int totalReply = userDao.getTotalRepbly(bloid);
		String startPage = request.getParameter("pageno");
		int totalPage = (totalReply % pageSize == 0) ? (totalReply / pageSize) : (totalReply / pageSize + 1); 
		if(startPage != null && !startPage.trim().equals("")) {
			pageNo = Integer.parseInt(startPage);
		} else {
			pageNo = 1;
		}
		if(pageNo <= 1) {
			pageNo = 1;
		}
		if(pageNo >= totalPage && totalPage != 0) {
			pageNo = totalPage;
		}
		startPos = (pageNo - 1) * pageSize;
		BBSBlog bbsBlog= new BBSBlog();
		List<BBSReplyBlog> bbsReplyList = new ArrayList<BBSReplyBlog>(); 
		bbsBlog = userDao.getBlogDetail(bloid);
		bbsReplyList = userDao.getReplyBlog(bloid, startPos, pageSize);
		request.setAttribute("pageno", pageNo);
		request.setAttribute("bloid", bloid);
		request.setAttribute("cboname", request.getParameter("cboname"));
		request.setAttribute("bbsblog", bbsBlog);
		if(bbsReplyList != null) {
			request.setAttribute("bbsreplylist", bbsReplyList);
		}
		request.setAttribute("cboid", request.getParameter("cboid"));
		return "f:/detail.jsp";
	}
	
	public String postBlog(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String cboid = request.getParameter("cboid");
		String uname = request.getSession().getAttribute("uname").toString();
		String blotitle = request.getParameter("blotitle");
		String blocontent = request.getParameter("blocontent");
		BBSBlog bbsBlog = new BBSBlog();
		bbsBlog.setBlotitle(blotitle);
		bbsBlog.setUname(uname);
		bbsBlog.setBlocontent(blocontent);
		bbsBlog.setCboid(cboid);
		request.setAttribute("cboid", cboid);
		request.setAttribute("cboname", request.getParameter("cboname"));
		userDao.post(bbsBlog);
		return "f:/userservice?method=listBlog";
	}
	
	public String replyBlog(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String rblContent = request.getParameter("rblcontent");
		String uname = request.getSession().getAttribute("uname").toString();
		int bloid = Integer.parseInt(request.getParameter("bloid"));
		BBSReplyBlog bbsReplyBlog = new BBSReplyBlog();
		bbsReplyBlog.setRblContent(rblContent);
		bbsReplyBlog.setUname(uname);
		bbsReplyBlog.setBloid(bloid);
		userDao.reply(bbsReplyBlog);
		request.setAttribute("bloid", bloid);
		return "f:/userservice?method=blogDetail";
		
	}
}
